package com.example.nuc_ms_beta.ui.viewconstruction.moderfragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.nuc_ms_beta.R;
import com.example.nuc_ms_beta.app.MyApplication;
import com.example.nuc_ms_beta.bean.competeactivity.Compete_ListView;
import com.example.nuc_ms_beta.ui.activity.CompeteDetailActivity;
import com.example.nuc_ms_beta.ui.viewconstruction.adapter.CompeteActivityListViewAdapter;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;


/**
 * Created by 张博川 on 2016/9/28. 赛事活动
 */
public class CompeteActivityFragment extends Fragment {
    private View view;//主View
    private ListView competeactivityfragment_listview;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(inflater, container);
        return view;
    }

    private void initView(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.competeactivityfragment, container, false);
        competeactivityfragment_listview = (ListView) view.findViewById(R.id.competeactivityfragment_listview);
        queryData();
    }

    /**
     * Query the data for CompeteActivity
     */
    private void queryData() {

        BmobQuery<Compete_ListView> query_listView = new BmobQuery<>();
        query_listView.findObjects(new FindListener<Compete_ListView>() {

            @Override
            public void done(final List<Compete_ListView> list, BmobException e) {
                if (e == null) {
                    competeactivityfragment_listview.setAdapter(new CompeteActivityListViewAdapter(getContext(), list, R.layout.competeactivity_listview_item));
                    competeactivityfragment_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(getContext(), CompeteDetailActivity.class);
                            intent.putExtra("imageUrl", list.get(position).getListview_bg_img().getFileUrl());
                            intent.putExtra("topic", list.get(position).getListview_title());
                            intent.putExtra("type", list.get(position).getObjectId());
                            startActivity(intent);
                        }
                    });
                } else {
                    Log.d(MyApplication.TAG, e.getErrorCode() + " " + e.getMessage());
                }
            }
        });
    }

    public static CompeteActivityFragment getInstance() {
        CompeteActivityFragment fragment = new CompeteActivityFragment();
        return fragment;
    }
}

